package br.com.classificadosonline.persistencia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Conexao {
	private static Connection con;
	private static Statement stmt;
	/*
	public static void update(String query) {
		int numUpd;
		InitialContext ic;
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
			ic = new InitialContext();
			DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/DB");
			con = ds.getConnection();
			stmt = con.createStatement();							
			numUpd = stmt.executeUpdate(query);
			stmt.close();
			con.close();
		} catch(SQLException e) { 
			System.out.println(e.getMessage());
		} catch (NamingException e) {
			System.out.println(e.getMessage());
		} catch(java.lang.ClassNotFoundException e) {
			System.out.println(e.getMessage());
		}
	}	
	*/
	public static void update(String query) {
		int numUpd;
		try { 
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
		} catch(java.lang.ClassNotFoundException e) {
		}
		try {
			con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=CLASSIFICADOS","sa","senha");
			stmt = con.createStatement();							
			numUpd = stmt.executeUpdate(query);
			stmt.close();
			con.close();
		} catch(SQLException ex) { 
			System.out.println(ex.getMessage());
		}
	}
	
	public static ResultSet select(String query) {
		ResultSet rs = null;
		try { 
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
		} catch(java.lang.ClassNotFoundException e) {
		}
		try {
			con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=CLASSIFICADOS","sa","senha");
			stmt = con.createStatement();							
			rs = stmt.executeQuery(query);
			stmt.close();
			con.close();
		} catch(SQLException ex) { 
		}
		return rs;
	}
	
	public static Connection conexao () {
		try {
			con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=CLASSIFICADOS","sa","senha");
		} catch(SQLException ex) { 
		}
		return con;
	}
}
